package com.googlecode.gwtbb.client.shared.remote.picture;

import com.googlecode.gwtbb.client.shared.GwtbbSmess;
import com.googlecode.gwtbb.client.shared.entity.picture.Picture;
import com.googlecode.gwtbb.client.shared.util.StringUtils;
import com.googlecode.gwtbb.client.shared.validation.Failure;
import com.googlecode.gwtbb.client.shared.validation.FieldId;
import com.googlecode.gwtbb.client.shared.validation.GwtbbValidator;
import com.googlecode.gwtbb.client.shared.validation.Validateable;

import java.io.Serializable;
import java.util.Map;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Aug 2, 2008
 */
public class PictureItemBean implements Validateable, Serializable {
	private String fileName;
	private String caption;

	public PictureItemBean() {
	}

	public PictureItemBean(String fileName, String caption) {
		this.fileName = fileName;
		this.caption = caption;
	}

	public String getCaption() {
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public Map<FieldId, Failure> validate(Map<FieldId, Failure> failures, int index) {
		if (StringUtils.isNotEmpty(caption)) {
			// if the user specified a caption, they should also specify a picture
			if (StringUtils.isEmpty(fileName))
				failures.put(FieldId.create(Picture.ID, index), new Failure(GwtbbSmess.instance().required()));
			caption = GwtbbValidator.max(failures, FieldId.create(Picture.CAPTION, index), caption, Picture.CAPTION_MAX);
		}
		return failures;
	}
}